//
//  Kratos_userInfo.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/12.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_userInfo.h"
#import "YYModel.h"
#import "XTSecurityUtil.h"
#import "NSString+Base64.h"
#import <objc/runtime.h>
static NSString *fileName = @"rwrw";


NSMutableDictionary *propertyDict = nil;
@implementation Kratos_userInfo



- (instancetype)initWithDict:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        self.Kra_ID = dict[@"id"];
        self.Kra_username = dict[@"username"];
        self.Kra_nickname = dict[@"nickname"];
        self.Kra_thumb = dict[@"thumb"];
        self.Kra_subscribe = dict[@"subscribe"];
        self.Kra_logflag = dict[@"logflag"];
        self.Kra_fuid = dict[@"fuid"];
        self.Kra_instime = dict[@"instime"];
        self.Kra_type = dict[@"type"];
        self.Kra_token = dict[@"token"];
        self.Kra_chid = dict[@"chid"];
        self.Kra_gender = dict[@"gender"];
    }
    return self;
}

+ (void)Kratos_loginOut
{
    NSString *path =  fileName.Kratos_appendCacheDir_Kratos;
    if ([[NSFileManager defaultManager]fileExistsAtPath:path])
    {
        NSError *error = nil;
        
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        if (error == nil) {
        }else
        {
        }
    }
}


- (void)Kratos_saveinfo_Kratos
{
    NSDictionary *dic =  [self yy_modelToJSONObject];
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *encryStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSData *encryptedData = [XTSecurityUtil encryptDESData:encryStr];
    NSString *filePath =  fileName.Kratos_appendCacheDir_Kratos;
    if (encryptedData.length > 0)
    {
        [encryptedData writeToFile:filePath atomically:true];
    }else
    {
    }
    
}


+ (instancetype)Kratos_getCurrentUserInfo_Kratos
{
    NSString *path =  fileName.Kratos_appendCacheDir_Kratos;
    NSData *data = [[NSData alloc]initWithContentsOfFile:path];
    NSString *decryptDesStr =  [XTSecurityUtil decryptDESData:data];
    if (decryptDesStr.length == 0)
    {
            return  nil;
    }
    NSData *  decryptData = [decryptDesStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
   NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:decryptData options:NSJSONReadingAllowFragments error:&error];
    if (error != nil)
    {
        return  nil;
    }
    
    Kratos_userInfo * user =  [self yy_modelWithDictionary:dict];
    
    return user;
    
    
}

@end
